package cn.fanyamin.dao.hibernate;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class BaseDao {

   protected BaseDao() {
   }

   public static Session getSession() {
      Session session = (Session) BaseDao.session.get();
      if (session == null) {
         session = sessionFactory.openSession();
         BaseDao.session.set(session);
      }
      return session;
   }
   
   protected void begin() {
      getSession().beginTransaction();
   }
   
   protected void commit() {
      getSession().getTransaction().commit();
   }
   
   protected void rollback() {
      try {
      getSession().getTransaction().rollback();
      } catch( HibernateException e ) {
         log.log(Level.WARNING,"Cannot rollback",e);
      }
      
      try {
         getSession().close();
      } catch( HibernateException e ) {
         log.log(Level.WARNING,"Cannot close",e);         
      }
      BaseDao.session.set(null);
   }
   
   public static void close() {
      getSession().close();
      BaseDao.session.set(null);
   }
   
   private static final Logger log = Logger.getAnonymousLogger();

   private static final ThreadLocal session = new ThreadLocal();

   private static final SessionFactory sessionFactory = new Configuration()
         .configure().buildSessionFactory();
}
